//
//  QRCodeDisplayView.h
//  EPTool
//
//  Created by guoshiqing on 2018/4/27.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "QRCodeDisplayView.h"

@implementation QRCodeDisplayView
-(void)awakeFromNib
{
    [super awakeFromNib];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeQRCodeViewFromSuperview:)];
    [self addGestureRecognizer:tap];
}

+(void)createQRCodeViewWithQRImage:(UIImage *)image andOffset:(CGPoint)offSet andCloseBlock:(closeQRCodeView)block
{
    QRCodeDisplayView * QRCode = [[[UINib nibWithNibName:@"QRCodeDisplayView" bundle:nil] instantiateWithOwner:nil options:nil] firstObject];
    CGFloat height = QRCode.QRCodeViewHeightLayout.constant;
    QRCode.QRCodeViewHeightLayout.constant = 0;
    QRCode.frame = [UIScreen mainScreen].bounds;
    QRCode.QRCodeImageView.image = image;
    [UIView animateWithDuration:0.5f animations:^{
        if (offSet.y) {
            QRCode.QRCodeViewCenterYLayout.constant = QRCode.QRCodeViewCenterYLayout.constant + offSet.y;
        }
        QRCode.QRCodeViewHeightLayout.constant = height;
        [QRCode setNeedsLayout];
    }];
    [[[[UIApplication sharedApplication] delegate] window]  addSubview:QRCode];
    
    QRCode.block = block;
}


-(void)removeQRCodeViewFromSuperview:(UIGestureRecognizer *)gesture
{
    if (!CGRectContainsPoint(_QRCodeView.frame, [gesture locationInView:self]))
    {
        if (_block) {
            self.block(YES);
        }
        [self removeFromSuperview];
    }
}

- (IBAction)CloseTheQRView:(id)sender {
    if (_block) {
        self.block(YES);
    }
    [self removeFromSuperview];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
